<hr>
<p>title: 292.nim-game<br>date: 2021-10-21 15:27:23<br>tags:</p>
<pre><code>- LeeCode
</code></pre><p>categories: </p>
<pre><code>- LeeCode
</code></pre><p>hidden: true</p>
<h2 id="cateHidden-false"><a href="#cateHidden-false" class="headerlink" title="cateHidden: false"></a>cateHidden: false</h2><h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><blockquote>
<p>You are playing the following Nim Game with your friend:</p>
<p>Initially, there is a heap of stones on the table.</p>
<p>You and your friend will alternate taking turns, and you go first.</p>
<p>On each turn, the person whose turn it is will remove 1 to 3 stones from the heap. The one who removes the last stone is the winner.</p>
<p>Given n, the number of stones in the heap, return true if you can win the game assuming both you and your friend play optimally, otherwise return false.</p>
</blockquote>
<h3 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h3><pre><code class="lang-bash">Input: n = 4
Output: false
Explanation: These are the possible outcomes:
1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins.
2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins.
3. You remove 3 stones. Your friend removes the last stone. Your friend wins.
In all outcomes, your friend wins.
</code></pre>
<h3 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h3><p> 因为我先拿a（[1,3]）个，所以如果总数是4n（4的倍数），那么对方每次拿4-a个，他就一定可以赢我（对方很聪明）。<br> 所以如果总数是4的倍数，那么我肯定会输。</p>
<p>  <em>（这个游戏最重要的就是先拿的吃亏）</em></p>
<pre><code class="lang-js"> var canWinNim = function(n) {
    // 所有的情况内，是否存在赢的可能性
    return n % 4 !== 0
 }
</code></pre>
<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><blockquote>
<p>Accepted</p>
<p>60/60 cases passed (92 ms)</p>
<p>Your runtime beats 30.82 % of javascript submissions</p>
<p>Your memory usage beats 48.2 % of javascript submissions (38.6 MB)</p>
</blockquote>
